package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.app.Dialog
import android.os.CountDownTimer
import android.view.View
import com.cheyun.netsalev3.repository.LoginRepository
import android.view.Gravity
import android.view.ViewGroup
import android.view.LayoutInflater
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.utils.*
import com.cheyun.netsalev3.view.LoginActivity

class ChangePwdActivityViewModel : BaseViewModel() {

    var mobile: String = ""
    var oldPassword: String = ""
    var password: String = ""
    var confirmPassword: String = ""
    var code: String = ""


    var version:String = "";
    private var loginRepository = LoginRepository()
    var showOldPwd = MutableLiveData<Boolean>()
    var showPwd = MutableLiveData<Boolean>()
    var showConfirmPwd = MutableLiveData<Boolean>()
    var activity: Activity? = null
    var to:String = ""

    fun onClickRegister(view: View) {
        if (CommonFunc.isBlank(oldPassword)) {
            MyToast("请输入原始密码")
            return;
        }
        if (CommonFunc.isBlank(password)) {
            MyToast("请输入登录密码")
            return;
        }
        if(password.length<8 || password.length > 50){
            MyToast("密码请在8-50位之间")
            return
        }
        if(!CommonFunc.checkPwd(password)){
            MyToast("密码必须包含大写字母、小写字母、数字、特殊符号中的其中3种")
            return
        }
        if(CommonFunc.isBlank(confirmPassword)){
            MyToast("请输入确认密码")
            return;
        }
        if(!password.equals(confirmPassword)){
            MyToast("两次输入的密码不一致，请确认")
            return;
        }
        loginRepository.changePwd(oldPassword, password,confirmPassword, this::setData)
    }

    fun onOldPasViewClick(view:View){
        this.showOldPwd.postValue(!showOldPwd.value!!)
    }

    fun onPasViewClick(view:View){
        this.showPwd.postValue(!showPwd.value!!)
    }

    fun onConfirmPasViewClick(view:View){
        this.showConfirmPwd.postValue(!showConfirmPwd.value!!)
    }

    fun onBack(view:View){
        this.myFinish()
    }

    /**
     * 登陆请求数据回调
     */
    fun setData(param: Any) {
        this.activity?.finish()
        MyToast("密码重置成功,请重新登录")
        myStartActivity(MyIntent(LoginActivity::class.java))
    }


    var totalTime = 60000L
    var oneceTime = 1000L
    var showTime = MutableLiveData<String>()
    var currentTime = 0L
    private val countDownTimer = object : CountDownTimer(totalTime, oneceTime) {
        override fun onTick(millisUntilFinished: Long) {
            val value = (millisUntilFinished / 1000).toInt().toString()
            currentTime = millisUntilFinished/1000
            showTime.postValue(value)
        }

        override fun onFinish() {
            currentTime = 0
            showTime.postValue("0")
        }
    }

    fun login(view:View) {
        this.activity?.finish()
    }

    fun showYs(view:View){
        val bottomDialog = Dialog(view.context, R.style.BottomDialog)
        val contentView = LayoutInflater.from(view.context).inflate(R.layout.dialog_content_circle, null)
        bottomDialog.setContentView(contentView)
        val params = contentView.getLayoutParams() as ViewGroup.MarginLayoutParams
        params.width = view.context.getResources().getDisplayMetrics().widthPixels - FunctionUtils.dip2px(view.context, 16f)
        params.bottomMargin = FunctionUtils.dip2px(view.context, 8f)
        contentView.setLayoutParams(params)
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM)
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation)
        bottomDialog.show()
    }
}